﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.JCI.Common.Entity;
using Microsoft.JCI.Bll;
using System.Collections.Generic;
using Microsoft.JCI.Common.Utility;
using System.Text;

namespace Microsoft.JCI.Portal.Dispatch
{
    public partial class LogAdd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["WorkingVendorEngineer"] == null)
                {
                    if (Request.QueryString["OrderID"] == null)
                    {
                        btnOK.Enabled = false;
                    }
                }
                else
                { InitData(); }
            }
        }

        private void InitData()
        {
            DispatchBll bll = new DispatchBll();
            DisWorkingVendorEngineerEntity entity =null;
            if (Request.QueryString["WorkingVendorEngineer"] != null)
            {
                entity = bll.Get<DisWorkingVendorEngineerEntity>(new Guid(Request.QueryString["WorkingVendorEngineer"]));
                if (entity == null) return;

                if (entity.AT != DateTime.MinValue)
                    RadDatePicker_AT.SelectedDate = entity.AT; 
                     RadDatePicker_CheckHour.Value= entity.CheckHour ;
                //entity.Date = System.DateTime.Now;
                     if (entity.DT != DateTime.MinValue)
                         RadDatePicker_DT.SelectedDate = entity.DT;
                     txtEngineerName.Text = entity.EngineerName;

                     txtHRS.Value = entity.HRS;
                if(entity.LT != DateTime.MinValue)
                     RadDatePicker_LT.SelectedDate = entity.LT;
          
                      txtOT.Value =  entity.OT;
               
                     txtT.Value = entity.T ;

                     txtTT.Value = entity.TT;
                     txtWS.Value = entity.WS;
            }
        }
        protected void btnOK_Click(object sender, EventArgs e)
        {
            DispatchBll bll = new DispatchBll();
            DisWorkingVendorEngineerEntity entity = new DisWorkingVendorEngineerEntity();
            if (Request.QueryString["WorkingVendorEngineer"] != null)
            {
                entity = bll.Get<DisWorkingVendorEngineerEntity>(new Guid(Request.QueryString["WorkingVendorEngineer"]));
            }
            else
            {
                entity.WorkingVendorEngineer = Guid.NewGuid();
            entity.WorkingVendorOrderId = new Guid(Request.QueryString["OrderID"]);
            }
            if(RadDatePicker_AT.SelectedDate.HasValue)
                entity.AT = RadDatePicker_AT.SelectedDate.Value;
            if (RadDatePicker_CheckHour.Value.HasValue)
                entity.CheckHour = RadDatePicker_CheckHour.Value.Value;
            entity.Date = System.DateTime.Now;
            if(RadDatePicker_DT.SelectedDate.HasValue)
                entity.DT = RadDatePicker_DT.SelectedDate.Value;
            entity.EngineerName = txtEngineerName.Text;
            if (txtHRS.Value.HasValue)
                entity.HRS = txtHRS.Value.Value;
            if(RadDatePicker_LT.SelectedDate.HasValue)
                entity.LT = RadDatePicker_LT.SelectedDate.Value;
            if (txtOT.Value.HasValue)
                entity.OT = txtOT.Value.Value;
            if (txtT.Value.HasValue)
                entity.T = txtT.Value.Value;
            if (txtTT.Value.HasValue)
                entity.TT = txtTT.Value.Value;
            if (txtWS.Value.HasValue)
                entity.WS = txtWS.Value.Value;
 
            
            bll.InsertOrUpdate(entity);

            ClientScript.RegisterStartupScript(this.GetType(), "saveEngineerLogs", "CloseWindow();", true);
        }
    }
}
